package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.domain.mpVO.MPFalseClueVo;
import com.huike.clues.domain.vo.MpAssignmentVo;
import com.huike.clues.mapper.ActivityMapper;
import com.huike.clues.mapper.MpAssignRecordMapper;
import com.huike.clues.mapper.MpClueMapper;
import com.huike.clues.service.MpCluesService;
import com.huike.clues.utils.HuiKeCrmDateUtils;
import com.huike.common.core.domain.model.LoginUser;
import com.huike.common.entity.MPTbActivity;
import com.huike.common.entity.MPTbAssignRecord;
import com.huike.common.entity.MPTbBusiness;
import com.huike.common.entity.MPTbClue;
import com.huike.common.getUserId.TokenService;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author 何俊杰
 * @Date 2023-08-29 16:43
 */
@Service
public class MpCluesServiceImpl extends ServiceImpl<MpClueMapper, MPTbClue> implements MpCluesService {

	@Autowired
	private MpClueMapper mpClueMapper;

	@Autowired
	private MpAssignRecordMapper assignRecordMapper;

	@Autowired
	private ActivityMapper activityMapper;

	@Autowired
	private TokenService tokenService;

	@Autowired
	private HttpServletRequest request;

	/**
	 * 新增线索管理
	 *
	 * @param clue
	 * @return
	 */
	@Override
	public int add(MPTbClue clue) {
		clue.setCreateBy(SecurityUtils.getUsername());

		clue.setCreateTime(LocalDateTime.now());
		mpClueMapper.insert(clue);
		MPTbAssignRecord tbAssignRecord = new MPTbAssignRecord();
		tbAssignRecord.setAssignId(clue.getId());
		tbAssignRecord.setUserId(SecurityUtils.getUserId());
		tbAssignRecord.setUserName(SecurityUtils.getUsername());
		tbAssignRecord.setDeptId(SecurityUtils.getDeptId());
		tbAssignRecord.setCreateBy(SecurityUtils.getUsername());
		tbAssignRecord.setCreateTime(LocalDateTime.now());
		int rows = assignRecordMapper.insert(tbAssignRecord);

		return rows;
	}

	/**
	 * 批量分配
	 *
	 * @param mpAssignmentVo
	 * @return
	 */
	@Override
	public String assignment(MpAssignmentVo mpAssignmentVo) {
		for (Long id : mpAssignmentVo.getIds()) {
			assignRecordMapper.updateS(id, mpAssignmentVo.getUserId());
		}

		return "全部成功";

	}

	/**
	 * 查询线索管理列表
	 *
	 * @param mpTbClue
	 * @return
	 */
	@Override
	public List<MPTbClue> selectClue(MPTbClue mpTbClue) {
		return mpClueMapper.selectClue(mpTbClue);
	}

	/**
	 * 修改线索管理
	 *
	 * @param mpTbClue
	 * @return
	 */
	@Override
	public int updateMpTbClue(MPTbClue mpTbClue) {
		//创建对象补充数据
		MPTbClue clue = new MPTbClue();
		BeanUtils.copyBeanProp(mpTbClue, clue);

		//根据活动id查询活动详情
		MPTbActivity mpTbActivity = activityMapper.selectById(mpTbClue.getActivityId());

		//根据活动id查询创建id
		MPTbAssignRecord mpTbAssignRecord = assignRecordMapper.getUserId(clue.getId());

		//获取当前登录用户
		LoginUser loginUser = tokenService.getLoginUser(request);

		//补充activityInfo , activityName ,userId , updateBy
		clue.setActivityInfo(mpTbActivity.getInfo());
		clue.setName(mpTbActivity.getName());
		clue.setUserId(mpTbAssignRecord.getUserId());
		clue.setUpdateBy(loginUser.getUsername());

		//补充修改时间
		clue.setUpdateTime(LocalDateTime.now());

		return mpClueMapper.updateMpTbClue(clue);
	}

	/**
	 * 线索转商机
	 * @param id
	 * @return
	 */
	@Override
	public int changeBusiness(Long id) {
		//根据id查询线索
		MPTbClue mpTbClue = mpClueMapper.selectById(id);

		//根据id修改线索状态
		mpClueMapper.resetNextTimeAndStatus(id,MPTbClue.StatusType.TOBUSINESS.getValue());

		//拷贝数据
		MPTbBusiness mpTbBusiness = new MPTbBusiness();
		BeanUtils.copyBeanProp(mpTbClue,mpTbBusiness);
		mpTbBusiness.setStatus(MPTbClue.StatusType.UNFOLLOWED.getValue());
		mpTbBusiness.setClueId(id);
		mpTbBusiness.setNextTime(null);
		//调用本类的新增线索方法
		int rows = this.add(mpTbBusiness);

		//返回数据
		return rows;
	}

	/**
	 * 伪线索
	 * @param id
	 * @param falseClueVo
	 * @return
	 */
	@Override
	public String falseId(Long id, MPFalseClueVo falseClueVo) {

		return null;
	}
}
